package com.qqin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qqin.mapper.*;
import com.qqin.pojo.PageBean;
import com.qqin.pojo.Users;
import com.qqin.service.UsersService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Properties;

@Service
public class UsersServiceImpl implements UsersService {
    private AddressMapper addressMapper;
    private CheckMapper checkMapper;
    private GoodsMapper goodsMapper;
    private CommentMapper commentMapper;
    private ItemMapper itemMapper;
    private OrderMapper orderMapper;
    private UsersMapper usersMapper;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public UsersServiceImpl() {
        try {
            // 加载数据库配置文件
            Properties properties = new Properties();
            InputStream dbStream = Resources.getResourceAsStream("db.properties");
            properties.load(dbStream);

            // 加载MyBatis配置文件
            String resource = "mybatis-config.xml";
            InputStream mybatisStream = Resources.getResourceAsStream(resource);

            // 创建SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisStream, properties);

            // 获取SqlSession
            sqlSession = sqlSessionFactory.openSession();

            // 获取Mapper实例
            checkMapper = sqlSession.getMapper(CheckMapper.class);
            goodsMapper= sqlSession.getMapper(GoodsMapper.class);
            commentMapper= sqlSession.getMapper(CommentMapper.class);
            itemMapper= sqlSession.getMapper(ItemMapper.class);
            orderMapper= sqlSession.getMapper(OrderMapper.class);
            usersMapper= sqlSession.getMapper(UsersMapper.class);
            addressMapper= sqlSession.getMapper(AddressMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Users> users = usersMapper.selectAll();
        Page<Users> p = (Page<Users>)users;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public Users selectById(Integer userId) {
        return usersMapper.selectById(userId);
    }

    @Override
    public Users login(Users user) {
        List<Users> usersList = usersMapper.selectByAny(user);

        if(usersList.isEmpty()) {
            System.out.println("用户名或密码错误");

        } else if(usersList.get(0).getState() == 1) {
            System.out.println("账号已封禁");
        }

        return usersList.get(0);
    }

    @Override
    public void signOut(Integer userId) {
        Users u = usersMapper.selectById(userId);
        LocalDateTime nowTime = LocalDateTime.now();

        u.setUpdateTime(nowTime);
        usersMapper.updateById(u);
        sqlSession.commit();
    }

    @Override
    public void register(Users user) {
        Users u = usersMapper.selectByUsername(user.getUserUsername());
        if(u != null) {
            System.out.println("用户名已存在");
        }

        LocalDateTime nowTime = LocalDateTime.now();

        user.setCreateTime(nowTime);
        user.setUserType(1);
        user.setState(0);
        usersMapper.register(user);
        sqlSession.commit();
    }

    @Override
    public Users updateById(Users user) {           //cjy方法
        //调用写好的方法判断是否存在相同信息的用户，分别判断name和id
        Users u1 = usersMapper.selectByUsername(user.getUserUsername());
        Users u2 = usersMapper.selectById(user.getUserId());

        if(u1 != null && !u2.getUserUsername().equals(user.getUserUsername())) {
            System.out.println("用户名已存在");
        }

        LocalDateTime nowTime = LocalDateTime.now();
        user.setUpdateTime(nowTime);

        //更新个人信息
        usersMapper.updateById(user);
        sqlSession.commit();
        return user;
    }


}
